<template>
    <div class="base64-image-container">
        <h2 class="base64-image-title">BASE64转图片</h2>
        <div class="base64-image-content">
            <textarea class="base64-image-text" v-model="inputText" placeholder="请输入图片BASE64数据..."></textarea>
            <div class="base64-image-result">
                <input type="file" class="base64-image-input" accept="image/*" @change="selectHandle">
                <img class="base64-image-img" :src="inputText" v-show="showImg">
                <div class="base64-image-progress" v-show="showProgress">
                    <TextLoadingBar :progress="loadProgress" :text="progressText"></TextLoadingBar>
                </div>
            </div>
            <div class="base64-image-btns">
                <button class="light-2" @click="selectImage">选择图片</button>
            </div>
        </div>
    </div>
</template>

<script>
import TextLoadingBar from '@/components/specialeffects/TextLoadingBar.vue';

export default {
    name: "Base64ImageUtil",
    components: { TextLoadingBar },
    data() {
        return {
            showImg: false,
            showProgress: false,
            loadProgress: 0,
            progressText: "上传进度",
            inputText: '',
            imageInput: null,
            reader: null,
            image: null,
        }
    },

    methods: {
        selectImage() {
            this.imageInput.click();
        },
        loadHandle(e) {
            this.showProgress = false;
            this.inputText = e.target.result
        },
        progressHandle(e) {
            this.showProgress = true;
            this.loadProgress = e.loaded / e.total;
            this.progressText = "上传进度:" + Math.floor(this.loadProgress * 100) + "%";

        },
        imgLoadHandle(e) {
            this.showImg = true;
        },
        imgErrorHandle(e) {
            this.showImg = false;

        },
        selectHandle() {
            if (this.imageInput.files.length === 0) {
                return
            }
            const file = this.imageInput.files[0];
            this.reader.readAsDataURL(file);
        },
    },
    mounted() {
        this.imageInput = this.$el.querySelector(".base64-image-input");
        this.image = this.$el.querySelector(".base64-image-img");
        this.image.onerror = this.imgErrorHandle;
        this.image.onload = this.imgLoadHandle;
        this.reader = new FileReader()
        this.reader.onload = this.loadHandle;
        this.reader.onprogress = this.progressHandle;
    },
}
</script>

<style scoped>
@import url('@/assets/css/button/light-2.css');

.base64-image-container {
    width: 100vw;
    height: 100vh;
    display: flex;
    background-color: #a5a2a2;
    justify-content: center;
    align-items: center;
}

.base64-image-title {
    position: absolute;
    top: 0;
    margin: 0;
}

.base64-image-content {
    position: relative;
    width: 90%;
    height: 90%;
    background-color: #d5d2d2;
    box-shadow: 0 0.25rem 1.125rem rgba(76, 92, 70, 0.1);
    border-radius: 5px;
}

.base64-image-text {
    position: absolute;
    height: 70%;
    width: 45%;
    font-size: 1.5rem;
    resize: none;
    margin: 5px;
    border: #1a1a1b 2px solid;
    border-radius: 5px;
    overflow-wrap: break-word;
    text-align: start;
}

.base64-image-result {
    position: absolute;
    height: 70%;
    width: 45%;
    margin: 10px;
    border: #2e71dd 2px solid;
    border-radius: 5px;
    background-color: #c6bfbf;
    right: 0%;
}

.base64-image-input {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    user-select: none;
    cursor: pointer;
}

.base64-image-img {
    position: absolute;
    width: 100%;
    height: 100%;
}

.base64-image-progress {
    position: absolute;
    width: 100%;
    height: 100%;
}

.base64-image-btns {
    position: absolute;
    bottom: 10%;
    right: 20%;
}

.base64-image-btns>button {
    position: relative;
    margin: 20px;
    font-size: 25px;
    border: 0;
    text-decoration: none;
    user-select: none;
    cursor: pointer;
}
</style>